<template>
    <div class="user-profile">
        <div class="user-profile-content">
            <a class="user-profile-avatar" href="http://cn.gravatar.com/" target="_blank" :style="{
                'background-image': avatar
                }"></a>
            <div class="user-profile-info">
                <div class="user-profile-row">
                    <label>用户名：</label>
                    <span class="user-profile-account">
                        {{ data.email&&data.email.slice(0, data.email.indexOf('@'))}}
                    </span>
                </div>
                <!-- <div class="user-profile-row"> -->
                    <!-- <div class="user-profile-setting">
                        <span><i class="iconfont icon-profile"></i> {{ data.plan ? data.plan.alias : '免费版' }}用户</span>
                        <g-button v-if="showPersonalCenterUpgrade" type="text" size="large" @click="upgrade">升级方案</g-button>
                    </div> -->
                    <!-- <div class="user-profile-setting">
                        <span><i class="iconfont icon-user"></i> 已绑定手机 134****4990</span> <g-button type="text" size="large">绑定手机</g-button>
                    </div> -->
                <!-- </div> -->
                <div class="user-profile-row">
                    <span class="user-profile-label">昵称:</span>
                    <div class="user-profile-nickname">
                        <g-input type="text" v-model="nickname" spellcheck="false"></g-input>
                        <g-button type="primary" 
                            @click="updateNickname" 
                            :disabled="!this.nickname || this.nickname.length === 0 || this.nickname.length > 15">修改</g-button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { user as userAPI } from '@/api'
import GInput from '@/components/common/g-input'
import GButton from '@/components/common/g-button'
import md5 from 'crypto-js/md5'

export default {
    name: 'user-profile',
    props: {
        data: {
            default () {
                return {}
            }
        }
    },
    watch: {
        data: {
            deep: true,
            handler ( val ) {
                this.nickname = val.name;
                this.updateAvatar();
            }
        }
    },
    mounted () {
        this.updateAvatar();
    },
    data () {
        let componentConf = this.$root && this.$root.componentConf ? this.$root.componentConf : null;
        let showPersonalCenterUpgrade = componentConf && componentConf.personal_center ? 
                componentConf.personal_center.solution_management : true;
        return {
            nickname: this.data.name,
            avatar: '',
            showPersonalCenterUpgrade: showPersonalCenterUpgrade
        }
    },
    methods: {
        updateAvatar() {
            const defaultAvatar = 'http://odqlwni8g.bkt.clouddn.com/geohey/static/img/default-avatar.png';
            if ( !this.data || !this.data.email ) { 
                this.avatar = `url(${ defaultAvatar })`;
            } else {
                this.avatar = `url(https://www.gravatar.com/avatar/${ md5( this.data.email.toLowerCase() ) }?d=${ encodeURI( defaultAvatar ) }&s=80)`
            }
        },
        upgrade () {
            this.$router.replace( { name: 'User', params: { name: 'plan' } } )
        },
        updateNickname () {
            if ( this.nickname.length === 0 || this.nickname.length > 15 ) {
                this.$message.error( '昵称长度为1-15个字符' );
                return;
            }

            userAPI.updateNickname( this.nickname ).then( res => {
                if ( res.data.code === 0 ) {
                    this.$message.success( '已修改昵称' );
                    this.$root.geohey.user.name = this.nickname;
                    this.$emit( 'on-change' );
                } else {
                    this.$message.error( res.data.msg || '修改昵称失败' );
                }
            } ).catch( err => {
                this.$message.error( '修改昵称失败' );
            } )

        }
    },
    components: {
        GButton,
        GInput
    }
}
</script>

<style lang="scss">
@import 'common';
.user-profile {
    
    .user-profile-content {
        min-width: 300px;
    
        .user-profile-avatar {
            display: block;
            background: $bgGray;
            border: 1px solid rgba(48,59,70,0.10);
            width: 82px;
            height: 82px;
            border-radius: 50%;
            float: left;
            margin: 10px 20px;
            background-size: cover;
        }

        .user-profile-info {
            height: 300px;
            overflow: hidden;
            padding-left: 40px;

            .user-profile-row {
                height: 60px;
                line-height: 60px;
                &::after {
                    @include clearfix();
                }

                .user-profile-label {
                    float: left;
                    color: $deepGray;
                    display: inline-block;
                    margin-right: 20px;
                }

                .user-profile-account {
                    color: $primary;
                    cursor: pointer;
                }

                .user-profile-setting {
                    display: inline-block;
                    margin-right: 20px;

                    .g-button {
                        font-size: $fontSizeBase;
                        margin-left: 10px;
                    }
                }

                .user-profile-nickname {
                    display: inline-block;

                    &::after {
                        @include clearfix();
                    }

                    .g-input {
                        float: left;
                        margin: 15px 0;
                        display: inline-block;
                    }

                    .g-button {
                        float: left;
                        margin: 16px 10px;
                    }
                }
            }
        }

        @media screen and (max-width: 650px) {
            & {
                text-align: center;
            }

            .user-profile-avatar {
                float: none;
                display: inline-block;
            }

            .user-profile-info {
                padding-left: 10px;
                padding-right: 10px;
                margin-top: 30px;
                width: 100%;
                text-align: left;

                .user-profile-row {
                    position: relative;

                    .user-profile-nickname {
                        position: absolute;
                        left: 52px;

                        .g-input {
                            width: 100%;
                            max-width: 300px;
                            padding: 0 75px 0 0;
                        }

                        .g-button {
                            position: absolute;
                            top: 0;
                            right: 0;
                        }

                    }
                }
            }
        }

    }

}
</style>
